package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.UserConstants;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import com.heima.user.service.ApUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 谢金成
 * @date 2024/4/27 17:29
 */
@Service
@Slf4j
@Transactional
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Autowired
    private ApUserService apUserService;

    /**
     * 查询认证列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(AuthDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 1.校验参数
        dto.checkParam();

        // 构建分页
        IPage<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        page = page(page, Wrappers.<ApUserRealname>lambdaQuery()
                .eq(ObjectUtils.isNotEmpty(dto.getStatus()), ApUserRealname::getStatus, dto.getStatus())
                .eq(dto.getStatus() != null, ApUserRealname::getStatus, dto.getStatus()));

        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    /**
     * 审核通过
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authPass(AuthDto dto) {
        // 1.检查参数
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 2.查询数据
        ApUserRealname apUserRealname = getOne(Wrappers.<ApUserRealname>lambdaQuery()
                .eq(ApUserRealname::getId, dto.getId()));

        if (apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        // 3.更新数据
        apUserRealname.setStatus(ApUserRealname.Status.AUTH_SUCCESS.getValue());
        apUserRealname.setUpdatedTime(new Date());
        updateById(apUserRealname);

        // 4.异步发送, 让自媒体端创建账号, (ap_user_id, name, password)
        ApUser apUser = apUserService.getOne(Wrappers.<ApUser>lambdaQuery()
                .eq(ApUser::getId, apUserRealname.getUserId()));
        if (apUser != null) {
            kafkaTemplate.send(UserConstants.ADD_WEMEDIA_USER_TOPIC, JSON.toJSONString(apUser));
        }

        return ResponseResult.okResult(null);
    }

    /**
     * 审核失败
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {
        // 1.校验参数
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.查询数据
        ApUserRealname apUserRealname = getOne(Wrappers.<ApUserRealname>lambdaQuery()
                .eq(ApUserRealname::getId, dto.getId()));

        if (apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        // 3.更新数据
        apUserRealname.setStatus(ApUserRealname.Status.AUTH_FAILED.getValue());
        apUserRealname.setReason(dto.getMsg());
        apUserRealname.setUpdatedTime(new Date());

        updateById(apUserRealname);
        // 4.返回结果
        return ResponseResult.okResult(null);
    }
}
